package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunSleepStateMapper;
import com.csun.cmny.provider.device.csun.sleep.SleepMessageSender;
import com.csun.cmny.provider.device.csun.sleep.entity.CsunSleepData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunSleepBind;
import com.csun.cmny.provider.model.domain.DmcCsunSleepState;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunSleepBindService;
import com.csun.cmny.provider.service.DmcCsunSleepStateService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.google.common.base.Preconditions;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-06-08 14:00
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcCsunSleepStateServiceImpl extends BaseService<DmcCsunSleepState> implements DmcCsunSleepStateService {

    @Resource
    private DmcCsunSleepStateMapper dmcCsunSleepStateMapper;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private SleepMessageSender sleepMessageSender;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置床垫是否采集数据
     *
     * @param deviceId the device id
     * @param state    the state 0-停止 1-开始
     * @return the int.
     */
    @Override
    public Integer sendDataMsg(Long deviceId, String state) {

        /*** 参数校验 ***/
        Preconditions.checkArgument(PublicUtil.isNotEmpty(deviceId),
                cmnyMessageSource.getMessage("device.id.null", null));

        // 判断设备是否为床垫
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunSleepByDeviceId(deviceId);

        // 判断床垫是否绑定
        String redisKey = RedisKeyUtil.getCsunSleepKey(dmcDevice.getDeviceNo());
        CsunSleepData csunSleepData = (CsunSleepData) redisTemplate.opsForValue().get(redisKey);
        if (PublicUtil.isNotEmpty(csunSleepData)) {
            csunSleepData.setState(state);
            redisTemplate.opsForValue().set(redisKey, csunSleepData);
        } else {
            throw new DmcBizException(ErrorCodeEnum.DMC100610036);
        }

        // 下发指令
        sleepMessageSender.sendDataMsg(dmcDevice, state);

        DmcCsunSleepState dmcCsunSleepState = new DmcCsunSleepState();
        dmcCsunSleepState.setDeviceId(deviceId);
        dmcCsunSleepState.setState(state);

        return dmcCsunSleepStateMapper.saveOrUpdate(dmcCsunSleepState);
    }
}
